package com.ybl.library;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import com.ybl.library.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 日期：16/6/22 14:15
 * <p/>
 * 描述：StepsViewIndicator 指示器
 */
public class ProInvestCycleStepsViewIndicator extends View {


 /*   private float mCompletedLineHeight;//完成线的高度     definition completed line height


    private Drawable mCompleteIcon;//完成的默认图片    definition default completed icon
    private Drawable mAttentionIcon;//正在进行的默认图片     definition default underway icon
    private Drawable mDefaultIcon;//默认的背景图  definition default unCompleted icon


    private float mLinePadding;//两条连线之间的间距  definition the spacing between the two circles


    private Paint mUnCompletedPaint;//未完成Paint  definition mUnCompletedPaint
    private Paint mCompletedPaint;//完成paint      definition mCompletedPaint
    private int mUnCompletedLineColor = ContextCompat.getColor(getContext(), R.color.uncompleted_color);//定义默认未完成线的颜色  definition
    private int mCompletedLineColor = Color.WHITE;//定义默认完成线的颜色      definition mCompletedLineColor
    private PathEffect mEffects;
    private int mComplectingPosition;//正在进行position   underway position
    private Path mPath;*/

    private OnDrawIndicatorListener mOnDrawListener;


    //定义默认的高度   definition default height
    private float defaultStepIndicatorNum;
    private float mCircleRadius; //圆的半径  definition circle radius
    private float mCenterY;//该view的Y轴中间位置     definition view centerY position
    private float mLeftY;//左上方的Y位置  definition rectangle LeftY position
    private float mRightY;//右下方的位置  definition rectangle RightY position
    private float mLineHeight;//连接两点间的线的高度
    private float mLineWidth;//连接两点间线的宽度
    private float mSpace;//线与点之间的间隔

    private int screenWidth;//this screen width
    private int mStepNum = 0;
    private List<Integer> mCircleColorList; //点的颜色list
    private List<Float> mCircleCenterPointPositionList;//定义所有圆的圆心点位置的集合 definition all of circles center point list

    private Paint mCirclePaint, mLinePaint;


    /**
     * 设置监听
     *
     * @param onDrawListener
     */
    public void setOnDrawListener(OnDrawIndicatorListener onDrawListener) {
        mOnDrawListener = onDrawListener;
    }

    /**
     * get圆的半径  get circle radius
     *
     * @return
     */
    public float getCircleRadius() {
        return mCircleRadius;
    }


    public ProInvestCycleStepsViewIndicator(Context context) {
        this(context, null);
    }

    public ProInvestCycleStepsViewIndicator(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProInvestCycleStepsViewIndicator(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    /**
     * init
     */
    private void init() {
        mCircleColorList = new ArrayList<>();
        mCircleCenterPointPositionList = new ArrayList<>();//初始化

        mCirclePaint = new Paint();
        mLinePaint = new Paint();

        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setStyle(Paint.Style.FILL);
        mCirclePaint.setStrokeWidth(2);

        mLinePaint.setAntiAlias(true);
        mLinePaint.setStyle(Paint.Style.FILL);
        mLinePaint.setStrokeWidth(2);

        defaultStepIndicatorNum = Utils.getDip(getContext(), 20);
        mCircleRadius = Utils.getDip(getContext(), 4);
        mLineHeight = Utils.getDip(getContext(), 2);
        mSpace = Utils.getDip(getContext(), 2);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = (int) (3 * defaultStepIndicatorNum);

        if (MeasureSpec.UNSPECIFIED != MeasureSpec.getMode(widthMeasureSpec)) {
            screenWidth = MeasureSpec.getSize(widthMeasureSpec);
            width = screenWidth;
        }

        int height = (int) defaultStepIndicatorNum;
        if (MeasureSpec.UNSPECIFIED != MeasureSpec.getMode(heightMeasureSpec)) {
            height = Math.min(height, MeasureSpec.getSize(heightMeasureSpec));
        }

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //获取中间的高度,目的是为了让该view绘制的线和圆在该view垂直居中   get view centerY，keep current stepview center vertical
        mCenterY = 0.5f * getHeight();
        //获取左上方Y的位置，获取该点的意义是为了方便画矩形左上的Y位置
        mLeftY = mCenterY - (mLineHeight / 2);
        //获取右下方Y的位置，获取该点的意义是为了方便画矩形右下的Y位置
        mRightY = mCenterY + mLineHeight / 2;
        mLineWidth = screenWidth - (getPaddingLeft() + getPaddingRight()) -
                mStepNum * mCircleRadius * 2 - (mStepNum - 1) * mSpace * 2;

        mCircleCenterPointPositionList.clear();
        float paddingLeft = getPaddingLeft();
        for (int i = 0; i < mStepNum; i++) {
            mCircleCenterPointPositionList.add(paddingLeft + mCircleRadius + i * mCircleRadius * 2 + i * mLineWidth
                    + i * mSpace * 2);
        }

        /**
         * set listener
         */
        if (mOnDrawListener != null) {
            mOnDrawListener.ondrawIndicator();
        }
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mOnDrawListener != null) {
            mOnDrawListener.ondrawIndicator();
        }

        //-----------------------画线-------draw line-----------------------------------------------
        for (int i = 0; i < mCircleCenterPointPositionList.size() - 1; i++) {
            //前一个ComplectedXPosition
            final float preComplectedXPosition = mCircleCenterPointPositionList.get(i);
            //后一个ComplectedXPosition
            final float afterComplectedXPosition = mCircleCenterPointPositionList.get(i + 1);

            RectF lineRect = new RectF(preComplectedXPosition + mCircleRadius + mSpace, mLeftY,
                    afterComplectedXPosition - mCircleRadius - mSpace, mRightY);

            Shader shader = new LinearGradient(lineRect.left, lineRect.top, lineRect.right, lineRect.bottom,
                    new int[]{mCircleColorList.get(i), mCircleColorList.get(i + 1)},
                    null, Shader.TileMode.REPEAT);
            mLinePaint.setShader(shader);

            canvas.drawRect(lineRect, mLinePaint);
        }
        //-----------------------画线-------draw line-----------------------------------------------


        //-----------------------画图标-----draw icon-----------------------------------------------
        for (int i = 0; i < mCircleCenterPointPositionList.size(); i++) {
            final float currentComplectedXPosition = mCircleCenterPointPositionList.get(i);
      /*      Rect rect = new Rect(
                    (int) (currentComplectedXPosition - mCircleRadius),
                    (int) (mCenterY - mCircleRadius),
                    (int) (currentComplectedXPosition + mCircleRadius),
                    (int) (mCenterY + mCircleRadius)
            );
            */
            mCirclePaint.setColor(Color.WHITE);
            canvas.drawCircle(currentComplectedXPosition, mCenterY, mCircleRadius * 1.1f, mCirclePaint);

            mCirclePaint.setColor(mCircleColorList.get(i));
            canvas.drawCircle(currentComplectedXPosition, mCenterY, mCircleRadius, mCirclePaint);
        }
        //-----------------------画图标-----draw icon-----------------------------------------------
    }

    /**
     * 得到所有圆点所在的位置
     *
     * @return
     */
    public List<Float> getCircleCenterPointPositionList() {
        return mCircleCenterPointPositionList;
    }

    /**
     * 设置流程步数
     *
     * @param circleColorList 流程步数颜色
     */
    public void setStepNum(List<Integer> circleColorList) {
        this.mCircleColorList = circleColorList;
        this.mStepNum = circleColorList.size();
        requestLayout();
    }


    /**
     * 设置对view监听
     */
    public interface OnDrawIndicatorListener {
        void ondrawIndicator();
    }


}
